/*
*/

package rosas.lou.weatherclasses;

import java.util.*;
import java.lang.*;
import rosas.lou.weatherclasses.*;
import gnu.io.*;

import com.dalsemi.onewire.*;
import com.dalsemi.onewire.adapter.*;
import com.dalsemi.onewire.container.*;

public class MissionLog{
   //Default Logging Rate set to 600 sec (10 min.)
   public static final int DEFAULT_LOGGING_RATE = 600;
   //Default Start Delay = 10 sec-->delay 10 secs before starting
   //a new mission
   private static final int DEFAULT_START_DELAY = 10;
   private int temperatureDataCount;
   private int humidityDataCount;
   //Instantiate as a LinkedList
   private List<Double> temperatureLog;
   //Instantiate as a LinkedList
   private List<Double> humidityLog;
   OneWireContainer41 owc41  = null;
   OneWireContainer22 owc22  = null;
   
   //*********************Public Methods***************************
   /*
   */
   public MissionLog(OneWireContainer41 currentOwc){
      this.setOneWireContainer41(currentOwc);
      this.temperatureLog = new LinkedList<Double>();
      this.humidityLog    = new LinkedList<Double>();
   }
   
   /*
   */
   public void clearLog() throws MemoryException{
      //TBD:  Refer to Use Case I1
      OneWireContainer41 owc = this.getOneWireContainer41();
      try{
         //Need to stop Mission First!!
         this.stopLogging();
         owc.clearMissionResults();
      }
      catch(MissionException me){
         throw new MemoryException(me.getMessage());
      }
      catch(NullPointerException npe){
         throw new MemoryException(npe.getMessage());
      }
      catch(OneWireIOException ioe){
         throw new MemoryException(ioe.getMessage());
      }
      catch(OneWireException   owe){
         throw new MemoryException(owe.getMessage());
      }
   }
   
   /*
   */
   public OneWireContainer41 getOneWireContainer41(){
      return this.owc41;
   }
   
   /*
   */
   public OneWireContainer22 getOneWireContainer22(){
      return this.owc22;
   }
   
   /*
   Start a new mission with the default logging rate:  10 Min,
   the default start delay:  10 sec
   */
   public void startLogging() throws MissionException{
      this.startLogging(DEFAULT_LOGGING_RATE, DEFAULT_START_DELAY);
   }
   
   /*
   Start a new mission with a desired logging rate, and the default
   start delay:  10 sec
   */
   public void startLogging(int sampleRate) throws MissionException{
      this.startLogging(sampleRate, DEFAULT_START_DELAY);
   }
   
   /*
   Start a new mission with the desired logging rage and desired
   start delay.
   
   Lots more to write here!
   First, this method is NOT complete!  There is so much more to do
   with this method!
   This is what I call the "Default" type method!  I started very
   easy at the start!
   As more functionality is added to the application, I suspect 
   either this method will need improvement OR other methods will
   need to be added to handle the extra functionality.  I am going
   to have to think long and hard about that.
   Here is a list of functionality NOT added to this method (and to
   the current app)
   1)  No High Or Low Temperature Alarm Settings
   2)  No High or Low Humidity Alarm Settings
   3)  No Mission Resolution for EITHER high or low settings:  I am
       currently ONLY using the default settings
   4)  No Independent setting of the channels (Temperature or
       humidity)
   As stated earlier, all of this will EVENTUALLY have to go into
   the functionality of the application:  be that in this method, or
   others.
   I am not quiet sure how to accomplish this feat just yet.
   */
   public void startLogging
   (
      int sampleRate,
      int startDelay
   ) throws MissionException{
      OneWireContainer41 owc = this.getOneWireContainer41();
      try{
         //First, if a mission is in progress, go ahead and stop
         //the mission
         this.stopLogging();
         //Get the total number of mission channels, first
         boolean[] channels = 
                        new boolean[owc.getNumberMissionChannels()];
         //Lots more setting (probably) to go in here...TBD...
         //enable the temperature channel (log temperature data)
         channels[owc.TEMPERATURE_CHANNEL] = true;
         //enable the humidity channel (log humidity data)
         channels[owc.DATA_CHANNEL]        = true;
         //Start a new mission with the sample rate, the delay time,
         //By default, NO rollover enabled, by default, the clock
         //synched and by default, BOTH the Temperature and Humidity
         //channels enabled
         owc.startNewMission(sampleRate, startDelay,
                                             false, true, channels);
      }
      catch(NullPointerException npe){
         throw new MissionException(npe.getMessage());
      }
      catch(OneWireIOException ioe){
         throw new MissionException(ioe.getMessage());
      }
      catch(OneWireException owe){
         throw new MissionException(owe.getMessage());
      }
   }
   
   /*
   */
   public void stopLogging() throws MissionException{
      OneWireContainer41 owc = this.getOneWireContainer41();
      try{
         owc.stopMission();
      }
      catch(NullPointerException npe){
         throw new MissionException(npe.getMessage());
      }
      catch(OneWireIOException ioe){
         throw new MissionException(ioe.getMessage());
      }
      catch(OneWireException   owe){
         throw new MissionException(owe.getMessage());
      }
   }
   
   //**********************Private Methods*************************
   /*
   */
   private void setOneWireContainer41
   (
      OneWireContainer41 currentOwc
   ){
      this.owc41 = currentOwc;
   }
   
   /*
   */
   private void setOneWireContainer22
   (
      OneWireContainer22 currentOwc
   ){
      this.owc22 = currentOwc;
   }
}
